package net.zjitc.dao;

import net.zjitc.Utils.C3P0Utils;
import net.zjitc.domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/29.
 */
public class UserDaoInmpl implements UserDao{
    public User findByUsernameAndPassword(String username, String password) {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSourse());
        String sql ="select*from user where username=? and password=?";
        User user = null;
        try {
            user = (User) runner.query(sql,new BeanHandler(User.class),username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public User findByUsername(String username){
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSourse());
        String sql ="select * from user where username=?";
        try {
            return (User) runner.query(sql,new BeanHandler(User.class),username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void insert(User user){
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSourse());
        String sql ="insert into user(username,password,phone,email,address) values(?,?,?,?,?)";
        try {
            runner.update(sql,user.getUsername(),user.getPassword(),user.getPhone(),user.getEmail(),user.getAddress());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
